﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPDPWEM.Utils
{
    /// <summary>
    /// Extensions to the List class.
    /// </summary>
    public static class ListExtensions
    {
        static Random _random = new Random();

        /// <summary>
        /// Shuffles the contents of the list using a Fisher-Yates shuffle.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        public static void Shuffle<T>(this List<T> items)
        {
            for (int i = items.Count-1; i > 1; i--)
            {
                SwapItems(items, i, _random.Next(0, i));
            }
        }

        /// <summary>
        /// Swaps the contents of the two locations in the list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <param name="indexA"></param>
        /// <param name="indexB"></param>
        public static void SwapItems<T>(this List<T> items, int indexA, int indexB)
        {
            T tempA = items[indexA];
            items[indexA] = items[indexB];
            items[indexB] = tempA;
        }
    }
}
